package souralven.jcomicreader.ui;

import java.awt.BorderLayout;

import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JMenuItem;
import javax.swing.JScrollPane;

import souralven.jcomicreader.controller.ComicController;
import souralven.jcomicreader.controller.listener.ExitListener;
import souralven.jcomicreader.controller.listener.OpenFileListener;
import souralven.jcomicreader.util.ImageUtil;
import souralven.jcomicreader.util.MessageUtil;

/**
 * @author Leonardo
 *
 */
public class ReaderFrame extends JFrame {

	/**
	 * 
	 */
	private static final long serialVersionUID = -1081655012777615225L;

	private JLabel statusBar = null;
	private ToolBar toolBar = null;
	private JMenu fileMenu = null;
	private JMenu helpMenu = null;
	private JMenuItem openMenuItem = null;
	private JMenuItem exitMenuItem = null;
	private JMenuItem aboutMenuItem = null;
	private JMenuBar jMenuBar = null;
	private ComicController controller = null;
	private ImageLabel imageLabel = null;
	private JScrollPane scrollPane = null;

	public ReaderFrame() {
		this.controller = new ComicController(this);
		initialize();
	}

	public void updateToolBarUI(final int imageNumber, final int max, final double zoomScale) {
	    toolBar.updateUI(imageNumber, max, zoomScale);
	}

    public void updateUI(final int imageNumber, final int max, final double zoomScale) {
        updateToolBarUI(imageNumber, max, zoomScale);
        getScrollPane().getHorizontalScrollBar().setValue(0);
        getScrollPane().getVerticalScrollBar().setValue(0);
    }

	private void initialize() {
		this.setIconImage(ImageUtil.getFavicon().getImage());
		this.setTitle(MessageUtil.getMessage("title"));
		this.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
		this.addWindowListener(new ExitListener(this));
		this.setLayout(new BorderLayout(5, 5));
		this.add(getScrollPane(), BorderLayout.CENTER);
		this.add(getStatusBar(), BorderLayout.SOUTH);
		this.add(getToolBar(), BorderLayout.NORTH);
		this.setJMenuBar(getJJMenuBar());
		this.setSize(320, 240);
		this.setLocationRelativeTo(null);
	}

	/**
	 * @return
	 */
	private JMenuBar getJJMenuBar() {
		if (jMenuBar == null) {
			jMenuBar = new JMenuBar();
			jMenuBar.add(getFileMenu());
			jMenuBar.add(getHelpMenu());
		}
		return jMenuBar;
	}

	/**
	 * @return
	 */
	public ImageLabel getImageLabel() {
		if (imageLabel == null) {
			imageLabel = new ImageLabel();
		}
		return imageLabel;
	}

	/**
	 * @return the statusBar
	 */
	private JLabel getStatusBar() {
		if (statusBar == null) {
			statusBar = new JLabel("");
		}
		return statusBar;
	}

	/**
	 * @return the menu
	 */
	private JMenu getFileMenu() {
		if (fileMenu == null) {
			fileMenu = new JMenu(MessageUtil.getMessage("file"));
			fileMenu.add(getOpenMenuItem());
			fileMenu.add(getExitMenuItem());
		}
		return fileMenu;
	}

	/**
	 * @return the helpMenu
	 */
	private JMenu getHelpMenu() {
		if (helpMenu == null) {
			helpMenu = new JMenu(MessageUtil.getMessage("help"));
			helpMenu.add(getAboutMenuItem());
		}
		return helpMenu;
	}

	/**
	 * @return the exitMenuItem
	 */
	private JMenuItem getExitMenuItem() {
		if (exitMenuItem == null) {
			exitMenuItem = new JMenuItem(MessageUtil.getMessage("exit"));
			exitMenuItem.addActionListener(new ExitListener(this));
		}
		return exitMenuItem;
	}

	/**
	 * @return the openMenuItem
	 */
	private JMenuItem getOpenMenuItem() {
		if (openMenuItem == null) {
			openMenuItem = new JMenuItem(MessageUtil.getMessage("open"));
			openMenuItem.addActionListener(new OpenFileListener(controller));
		}
		return openMenuItem;
	}

	/**
	 * @return the aboutMenuItem
	 */
	private JMenuItem getAboutMenuItem() {
		if (aboutMenuItem == null) {
			aboutMenuItem = new JMenuItem(MessageUtil.getMessage("about"));
		}
		return aboutMenuItem;
	}

	/**
	 * @return the toolBar
	 */
	private ToolBar getToolBar() {
		if (toolBar == null) {
			toolBar = new ToolBar(controller);
		}
		return toolBar;
	}

	/**
	 * @return
	 */
	public JScrollPane getScrollPane() {
		if (scrollPane == null) {
			scrollPane = new JScrollPane();
			scrollPane.setViewportView(getImageLabel());
		}
		return scrollPane;
	}
}
